<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">新生可视化管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="email">
                    <el-input v-model="param.email" placeholder="请输入18位身份证号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="param.password" placeholder="请输入密码" type="password">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <p style="display: flex; justify-content: space-between;">
                        <el-input v-model="param.code" placeholder="请输入验证码" style="width: 70%"><el-button slot="prepend" icon="el-icon-lx-people"></el-button></el-input>
                        <img ref="img" src="http://localhost:8989/student/code" alt="图片" srcset="" style="height:30px" @click="getCode" />
                    </p>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                    <el-button type="primary" @click="registerForm()">注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            param: {
                email: '111111111111111111',
                code:'',
                password:''
            },
            rules: {
                email: [{ required: true, message: '请输入18位身份证号', trigger: 'blur' }]
            }
        };
    },
    methods: {
        async getCode(){
            console.log(this.$refs.img.src);
            this.$refs.img.src="http://localhost:8989/student/code?tockenId=QWERTYUISDFGHJEW?"+Math.random();
        },
        submitForm() {
            const _this = this;
            if (_this.param.email.length < 18) {
                _this.$message.error('您输入的身份证号未满18位，请重新输入');
                return false;
            }
            this.$refs.login.validate(async valid => {
                if (valid) {
                    const res = await this.postJSON('/student/login', {
                        idCard: this.param.email,
                        code:this.param.code,
                        password:this.param.password
                    });
                    if (res.code === 200) {
                        localStorage.setItem('student', JSON.stringify(res.data));
                        localStorage.setItem('token', res.data.token);
                        this.$router.push('/home');
                        this.$message.success('登录成功');
                    } else {
                        this.$message.error(res.msg);
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        registerForm() {
            this.$router.push('/register');
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
    margin-left: 0px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
